// pages/read/read-detail/read-detail.js
var readsData = require("../../../data/posts-data.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic:false
  }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options)
      var readsId=options.id;
      //添加id 这个id 是页面的id  
      this.data.currentReadId = readsId;
      var readsDetail = readsData.postList[readsId];
      // 
      this.setData({
        readsDetail:readsDetail
      });

      var readsCollected =wx.getStorageSync("read_collected");
      console.log(readsCollected)
      if(readsCollected){
        var readCollected = readsCollected[readsId]
          this.setData({
            collected:readCollected
          })
      }else{
          var readsCollected = {};
          readsCollected[readsId] =false;
          wx.setStorageSync('read_collected', readsCollected)
      }

      console.log(readsData)
  },
  onColletionTap:function(){
    this.getReadsCollectedAsy();
    //同步
    // this.getReadsCollectedSync();
  },
  getReadsCollectedAsy:function(){
      // 异步还要停下
    var that = this;
      wx.getStorage({
        key: "read_collected",
        success: function (res) {
          var readsCollected = res.data;
          console.log(readsCollected);
          console.log(that.data);
          var readCollected = readsCollected[that.data.currentReadId];
          // 收藏变成未收藏，未收藏变成收藏 
          readCollected = !readCollected;
          readsCollected[that.data.currentReadId] = readCollected;
          that.showColl(readsCollected, readCollected);
        }
      })
  },
  //同步
  getReadsCollectedSync:function(){
      var readsCollected= wx.getStorageSync("read_collected");
      console.log(readsCollected)
      var readCollected= readsCollected[this.data.currentReadId]
      readCollected = !readCollected;
      readsCollected[this.data.currentReadId] =readCollected;
      this.showColl(readsCollected, readCollected)
  },
  showColl: function (readsCollected, readCollected){
      wx.setStorageSync("read_collected", readsCollected);
      this.setData({
        collected: readCollected
      });
      wx.showToast({
        title: readCollected?"收藏成功" :"取消收藏",
        icon: 'success',
        duration: 1000
      })
  },
  onShareTap:function(){
    wx.showActionSheet({
      itemList: ['QQ未开通', '微博', '您现在可以点击右上角分享给您的朋友'],
      success: function (res) {
        console.log(res.tapIndex)
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  onMusicTap:function(){
    var mousicData = readsData.postList[this.data.currentReadId];
    var isPlayingMusic = this.data.isPlayingMusic;
    if(isPlayingMusic){
        wx.pauseBackgroundAudio()
        this.setData({
          isPlayingMusic: false
        })
    }else{
        wx.playBackgroundAudio({
          dataUrl: mousicData.music.url,
          title: mousicData.music.title,
          coverImgUrl: mousicData.music.coverImg
        })
        this.setData({
          isPlayingMusic: true
        })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //   return {
  //     title: '离思五首·其四',
  //     desc: '曾经沧海难为水，除却巫山不是云',
  //     path: '/pages/read/read-detail/read-detail?id='+readsId
  //   }
  
  // }
})